package plus.feifei.attack.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.core.io.ClassPathResource;
import plus.feifei.attack.bean.ApiRequestBean;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ParseApiUtil {

    public static InputStream getInputStream(String path) throws Exception {
        return new ClassPathResource(path).getInputStream();
    }

    public static  List<ApiRequestBean> getApiList() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(getInputStream("api.json")));) {
            final List<ApiRequestBean> beanList = new ArrayList<>();

            StringBuilder sb = new StringBuilder();
            String str;
            while ((str = br.readLine()) != null) {
                // 跳过注释
                if (str.startsWith("--")) {
                    continue;
                }
                sb.append(str);
            }
            JSONArray arr = JSONUtil.parseArray(sb.toString());
            for (int i = 0; i < arr.size(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                ApiRequestBean bean = JSONUtil.toBean(obj, ApiRequestBean.class);
                beanList.add(bean);
            }
            return beanList;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }


    public static List<String> getPhoneList() {
        List<String> phoneList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(getInputStream("phone.text")));) {
            String str;
            while ((str = br.readLine()) != null) {
                // 跳过注释
                if (str.startsWith("--")) {
                    continue;
                }
                phoneList.add(str);
            }
            return phoneList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}
